package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.api.travelstandard.enums.AreaTypeEnum;
import com.corpgovernment.api.travelstandard.service.IMbTravelstandHotelPriceService;
import com.corpgovernment.api.travelstandard.vo.AreaConfigVo;
import com.corpgovernment.api.travelstandard.vo.MbTravelstandHotelPriceVo;
import com.corpgovernment.common.base.AbstractBaseService;
import com.corpgovernment.common.base.Page;
import com.corpgovernment.common.utils.ListUtils;
import com.corpgovernment.common.utils.ObjectUtils;
import com.corpgovernment.common.utils.PageUtils;
import com.corpgovernment.travelstandard.entity.db.MbTravelstandHotelPrice;
import com.corpgovernment.travelstandard.mapper.MbTravelstandHotelPriceMapper;
import com.ctrip.corp.obt.shard.context.PageContext;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MbTravelstandHotelPriceService extends AbstractBaseService implements IMbTravelstandHotelPriceService {

    @Autowired
    private MbTravelstandHotelPriceMapper mbTravelstandHotelPriceMapper;

    @Autowired
    private AreaConfigService areaConfigService;

    @Override
    public void insert(MbTravelstandHotelPriceVo model) {
        mbTravelstandHotelPriceMapper.insertSelective(ObjectUtils.copyProperties(model, MbTravelstandHotelPrice.class));
    }

    @Override
    public int delete(Long id) {
        return mbTravelstandHotelPriceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(MbTravelstandHotelPriceVo model) {
        return mbTravelstandHotelPriceMapper.updateByPrimaryKeySelective(ObjectUtils.copyProperties(model, MbTravelstandHotelPrice.class));
    }

    @Override
    public MbTravelstandHotelPriceVo get(Long id) {
        return ObjectUtils.copyProperties(mbTravelstandHotelPriceMapper.selectByPrimaryKey(id), MbTravelstandHotelPriceVo.class);
    }

    @Override
    public List<MbTravelstandHotelPriceVo> list(MbTravelstandHotelPriceVo model) {
        return ListUtils.copyList(mbTravelstandHotelPriceMapper.list(ObjectUtils.copyProperties(model, MbTravelstandHotelPrice.class)), MbTravelstandHotelPriceVo.class);
    }

    @Override
    public MbTravelstandHotelPriceVo selectOne(MbTravelstandHotelPriceVo model) {
        return ObjectUtils.copyProperties(mbTravelstandHotelPriceMapper.selectOne(ObjectUtils.copyProperties(model, MbTravelstandHotelPrice.class)), MbTravelstandHotelPriceVo.class);
    }

    @Override
    public int checkPriceUpperLimit(Long areaId) {
		return Optional.ofNullable(mbTravelstandHotelPriceMapper.checkPriceUpperLimit(areaId)).orElse(0);
    }

    @Override
    public Page page(MbTravelstandHotelPriceVo model) {
        MbTravelstandHotelPrice param = ObjectUtils.copyProperties(model, MbTravelstandHotelPrice.class);
        PageContext.startPage(model.getPageNum(), model.getPageSize());
        List<MbTravelstandHotelPrice> list = mbTravelstandHotelPriceMapper.list(param);
        return PageUtils.optPageBy(list, MbTravelstandHotelPriceVo.class);
    }

    public List<MbTravelstandHotelPriceVo> listAllTravelstandHotelPrice(Long tsHotelId, String orgId) {
        MbTravelstandHotelPriceVo model = new MbTravelstandHotelPriceVo();
        model.setTsHotelId(tsHotelId);
        List<MbTravelstandHotelPriceVo> travelstandHotelPriceVos = ListUtils.copyList(mbTravelstandHotelPriceMapper.list(ObjectUtils.copyProperties(model, MbTravelstandHotelPrice.class)), MbTravelstandHotelPriceVo.class);
        List<AreaConfigVo> otherAreas = areaConfigService.list(new AreaConfigVo().setAreaType(AreaTypeEnum.TYPE_1.getCode()).setOrgId(orgId).setStatus(NumberUtils.INTEGER_ZERO));
        if (otherAreas != null && otherAreas.size() > 0) {
            //获得其它地区
            AreaConfigVo otherArea = otherAreas.get(0);
            //对travelstandHotelPriceVos进行重排序
            for (MbTravelstandHotelPriceVo mbTravelstandHotelPriceVo : travelstandHotelPriceVos) {
                if (mbTravelstandHotelPriceVo.getTsHotelAreaId().equals(otherArea.getId())) {
                    travelstandHotelPriceVos.remove(mbTravelstandHotelPriceVo);
                    travelstandHotelPriceVos.add(mbTravelstandHotelPriceVo);
                    //一定要break，否则迭代器会抛并发异常
                    break;
                }
            }
        }
        return travelstandHotelPriceVos;
    }

    public int deleteByTsId(Long tsId) {
        return mbTravelstandHotelPriceMapper.deleteByTsId(tsId);
    }

    public int deleteByAreaId(Long areaId) {
        return mbTravelstandHotelPriceMapper.deleteByAreaId(areaId);
    }
}
